package com.isoft.service.blls.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.dao.CommonMetaDao;
import com.isoft.dao.blls.BllsDao;
import com.isoft.entity.CommonMeta;
import com.isoft.entity.ball.Blls;
import com.isoft.entity.flight.FlightInfo;
import com.isoft.entity.gate.Gate;
import com.isoft.service.blls.BllsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
/**
 * 行李服务层实现类
 */
@Service
public class BllsServiceImpl implements BllsService {
    @Autowired
    private BllsDao bllsDao;
    @Autowired
    private CommonMetaDao commonMetaDao;
    @Autowired
    private CommonMeta commonMeta;
    @Override
    public List<Blls> findAllBlls() {
        return bllsDao.findAllBlls();
    }

    /**
     * 根据METAID航班号去精确查询
     * @param METAID
     * @return
     */
    @Override
    public List<Blls> findBllsBySEQN(Integer METAID) {
        if(METAID != null){
        return bllsDao.findBllsBySEQN(METAID);
        }
        return null;
    }

    @Override
    public Boolean updateBlls(Blls blls) {
        return bllsDao.update(blls)>0;
    }

    @Override
    public Boolean deleteBlls(Integer ID) {
        Blls info = bllsDao.selectBllsByID(ID);
        int result = bllsDao.deleteById(ID);
        if(result>0){
            commonMetaDao.deleteCommonMeta(info.getMETAID());
        }
        return result>0;
    }

    /**
     * 添加
     * @param blls
     * @return
     */
    @Override
    public Boolean insertBlls(Blls blls) {
        if (commonMetaDao.selectAllSEQN(blls.getMETAID()) != null) {
            return false;
        }
        commonMeta.setSEQN(blls.getMETAID());
        commonMeta.setCREATETIME(new Date());
        commonMeta.setSNDR("FIMS");
        commonMeta.setDDTM(new Date());
        commonMeta.setTYPE("DFME");
        commonMeta.setSTYP("BLLS");
        commonMetaDao.insertCommonMeta(commonMeta);
        blls.setCREATE_TIME(new Date());
        return bllsDao.insert(blls)>0;
    }


    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @return
     */
    public IPage<Blls> getBllsInfosPage(int currentPage, int pageSize) {
        try {
            // 构建分页对象
            Page<Blls> page = new Page<>(currentPage, pageSize);

            LambdaQueryWrapper<Blls> queryWrapper = new LambdaQueryWrapper<>();

            queryWrapper.select(Blls::getMETAID, Blls::getID, Blls::getBTNO, Blls::getBELT_ID, Blls::getCODE, Blls::getESTR, Blls::getEEND, Blls::getVALID);
            queryWrapper.orderByDesc(Blls::getID); // 按ID降序排序
            Page<Blls> bllsPage = bllsDao.selectPage(page, queryWrapper);
            // 封装返回
            return bllsPage;
        } catch (Exception e) {
            // 异常处理逻辑，此处可根据需要抛出异常或处理后返回默认值
            throw new RuntimeException("获取数据失败：" + e.getMessage());
        }
    }
}
